package com.lht.cozyCloud.bee.controller;

import com.lht.cozyCloud.bee.domain.MessageRecord;
import com.lht.cozyCloud.bee.service.MessageRecordService;
import com.lht.cozyCloud.common.core.utils.poi.ExcelUtil;
import com.lht.cozyCloud.common.core.web.controller.BaseController;
import com.lht.cozyCloud.common.core.web.domain.AjaxResult;
import com.lht.cozyCloud.common.core.web.page.TableDataInfo;
import com.lht.cozyCloud.common.log.annotation.Log;
import com.lht.cozyCloud.common.log.enums.BusinessType;
import com.lht.cozyCloud.common.security.annotation.RequiresPermissions;
import com.lht.cozyCloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lht
 * @version 1.0
 * @description: TODO
 * @date 2025/1/3 9:44
 */

@RestController
@RequestMapping("/record")
public class MessageRecordController extends BaseController {
    @Autowired
    private MessageRecordService messageRecordService;

    @RequiresPermissions("bee:record:list")
    @GetMapping("/list")
    public TableDataInfo list(MessageRecord record) {
        startPage();
        List<MessageRecord> list = messageRecordService.selectMessageRecordList(record);
        return getDataTable(list);
    }

    @Log(title = "信息发送记录管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("bee:record:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MessageRecord record) {
        List<MessageRecord> list = messageRecordService.selectMessageRecordList(record);
        ExcelUtil<MessageRecord> util = new ExcelUtil<>(MessageRecord.class);
        util.exportExcel(response, list, "信息发送数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable Long recordId) {
        return success(messageRecordService.selectMessageRecordById(recordId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/record/{type}")
    public AjaxResult getTemplateByType(@PathVariable String type) {
        return success(messageRecordService.selectMessageRecordByType(type));
    }

//    /**
//     * 新增参数配置
//     */
//    @RequiresPermissions("bee:record:add")
//    @Log(title = "信息发送记录管理", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@Validated @RequestBody MessageRecord record) {
//        record.setCreateBy(SecurityUtils.getUsername());
//        return toAjax(messageRecordService.insertTemplate(record));
//    }

    /**
     * 修改参数配置
     */
    @RequiresPermissions("bee:record:edit")
    @Log(title = "模板管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody MessageRecord record) {
        record.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(messageRecordService.updateRecord(record));
    }

    /**
     * 删除参数配置
     */
    @RequiresPermissions("bee:record:remove")
    @Log(title = "模板管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds) {
        messageRecordService.deleteRecordByIds(recordIds);
        return success();
    }

}
